import * as NodeRSA from 'node-rsa'

const _priKey = `-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC909RTdlWpzpawJnK+qfvIQ0HQYEg5s5YO9idHcWhWbMpwt4kN
8T6KpRtySmSTr788PVKlCBLGM9R2PMkSYlxGriw+xbJs4nq/r/KCM9Zz8sgRV9/9
Tq8afwEpiU/ojoUWpO+3x0J5tErhoQh+5xRU78dLKaqC4q4JE6PASwPFkQIDAQAB
AoGAL+P/T0cteXerQKmSjVOpN+7v7bg8wIH0O7YSVs3fut4bfLBxHVldo/Dfqcfm
8/OE1ZfK/+O3iNWOpn3TZJX4HVVNPi2j2dYvbAKb0wUyClnCztcl5i20AzKdxqpy
hrzuuP+RhbBoOfe2Q+oQ4uQaWfoKqplLGjTGQZBrcSa7AaECQQDthXiazLtyU46Q
fnZWmHhJfPPb2KsMHH4PioFHXlnaafHQUTDfHdW0lCLXiXMD3rIgmOLgti7Jb5DZ
D8aDIAiFAkEAzJh5wnJhyq38atv5w5XffCBXVKC1c66+5DEt96rfoJsBbmv4Xn+V
WkmIP/4ePD/6QKncYQUevsijC4Y7tLAcnQJARqs+jfg6ETLxKOaneO5WY68Msy2M
/sZ/b0pLWpuk6ko5SzzM6KVszxSAJVbc5JrerrouTqOF2iMamqqJ6rueLQJBAIBN
Ff4UULMTTHmOnSBD+jQS6/EMHv3+lYLwPE1WAIrs6Ybfeo94HqbR+fl3WGqcDElE
vAizd0ZY97YgZYy37BkCQQDW/7EnBCeyr4SoAo+o6hlEJDaxBx1kwMuEKNG6WM/j
PcKXjP05DwthcL1LcRKLl970xz2n+qRPmt+Gj1BozJVB
-----END RSA PRIVATE KEY-----`

const privateKey = new NodeRSA(_priKey)
privateKey.setOptions({ encryptionScheme: 'pkcs1' }) // 因为jsencrypt自身使用的是pkcs1加密方案, nodejs需要修改成pkcs1。

export const encrypt = (txt: string): string => {
  return privateKey.encrypt(txt, 'base64')
}

/**
 * 后端node解密
 * @param {String} hash 加密的文本
 * @returns 
 */
export const decrypt = (hash: string) => {
  const decrypted = privateKey.decrypt(hash, 'utf8')
  return decrypted
}
